package com.libinshen.wxpay.videoAliBaBa.controller;

import com.libinshen.wxpay.common.enums.BaseResponseCodeEnum;
import com.libinshen.wxpay.common.exception.BaseException;
import com.libinshen.wxpay.common.vo.CommonResult;
import com.libinshen.wxpay.videoAliBaBa.entity.Video;
import com.libinshen.wxpay.videoAliBaBa.service.VideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lbs
 */
@Api(description = "课时管理")
@CrossOrigin //跨域
@RestController
@RequestMapping("/admin/edu/video")
public class VideoController {

    @Autowired
    private VideoService videoService;

    @ApiOperation(value = "新增课时")
    @PostMapping("save")
    public CommonResult save(
            @ApiParam(name = "videoForm", value = "课时对象", required = true)
            @RequestBody Video video) {

        boolean isSaved = videoService.save(video);
        if (isSaved) {
            return CommonResult.ok().setMessage("保存成功");
        } else {
            return CommonResult.error().setMessage("保存失败");
        }
    }

    @ApiOperation(value = "根据ID查询课时")
    @GetMapping("get/{id}")
    public CommonResult getById(
            @ApiParam(name = "id", value = "课时ID", required = true)
            @PathVariable String id) {
        if (StringUtils.isEmpty(id)) {
            throw new BaseException(BaseResponseCodeEnum.DATA_NOT_FOUND.getCode(), "id不能为空");
        }

        Video video = videoService.getById(id);
        if (video != null) {
            throw new BaseException(BaseResponseCodeEnum.DATA_NOT_FOUND.getCode(), "数据不存在");
        }
        return CommonResult.ok().data("item", video);
    }

    @ApiOperation(value = "更新课时")
    @PutMapping("update/{id}")
    public CommonResult updateCourseInfoById(
            @ApiParam(value = "课时基本信息", required = true)
            @RequestBody Video video) {

        boolean result = videoService.updateById(video);
        if (!result) {
            throw new BaseException(BaseResponseCodeEnum.DATA_NOT_FOUND.getCode(), "数据不存在");
        }
        return CommonResult.ok().setMessage("修改成功");
    }

    @ApiOperation(value = "根据ID删除课时")
    @DeleteMapping("{id}")
    public CommonResult removeById(
            @ApiParam(name = "id", value = "课时ID", required = true)
            @PathVariable String id) {

        //TODO 删除视频

        boolean result = videoService.removeById(id);
        if (result) {
            return CommonResult.ok();
        } else {
            return CommonResult.error().setMessage("数据不存在");
        }
    }
}
